Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

235
Vistas
React | error "Objects are not valid as a React child" when trying to push to state array

I'm learning some React with a project I dreamed up that has data being passed via props and also pubsub messages. I'm passing an email address via pubsub and it comes in fine on the pubsub subscribe method, like this:

myEmailChannel.subscribe("email", (msg) => {
            let data = msg.data;
            
            console.log(data);
                         
        });

The above results in a console log of

value: 'test@test.com'

test@test.com being a fake test email that I sent via pubsub. The problem happens when I try to add the data to an already initialized state array of "emails", I get some crazy error about "Objects are not valid as a React child (found: object with keys {value}). If you meant to render a collection of children, use an array instead." I am trying something like:

 myEmailChannel.subscribe("email", (msg) => {
            let data = msg.data;
            
            console.log(data);
            let emails = [...this.state.emails];
            emails.push({ value: data });
            this.setState({emails});
            
        });

only to get the aforementioned error. Actually, it happens no matter how I try to add the data to the array. I think I understand that I'm just formatting the incoming data incorrectly for the state array? Any help would be much appreciated.

EDIT: I followed the error a little further and it says the error occurs when it tries to iterate over the array for display:

{this.state.emails.map((email, index) => {
                return (

                    <li key={index}>{email}</li>

                );
            })}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

As you mentioned, email is an object. When you do something like this

this.state.emails.map((email, index) => (
  <li key={index}>{email}</li>
))

React gets confused. It doesn't know whether it should treat the email object as a string (which is properly what you are trying to do), or render it as a component.

You can do something like this instead.

this.state.emails.map((email, index) => (
  <li key={index}>{email.value}</li>
))
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda